/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package td1;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Enumeration;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/**
 * Réimplementation du JPanel pour etre une zone de dessin complete
 * @author saluzzo
 */
public class JavaCanvas extends JPanel implements MouseListener, MouseMotionListener, KeyListener {

    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_P:
                if (temp != null) {
                    temp.setZValue((temp.getZValue()) + 1);
                    this.Parent.getControleur().action("Canvas", "trierCollection", null, null);
                    this.repaint();
                }
                break;
            case KeyEvent.VK_M:
                if (temp != null) {
                    temp.setZValue((temp.getZValue()) - 1);
                    this.Parent.getControleur().action("Canvas", "trierCollection", null, null);
                    this.repaint();
                }
                break;
        }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void mouseClicked(MouseEvent e) {

        if (temp != null) {
            temp = null;
            this.setCursor(Cursor.getDefaultCursor());
            if (PointTemporaire != null) {
                PointTemporaire = null;

            }
        } else {
            for (Enumeration<Quadrilatere> en = this.Parent.getControleur().getCollection().elements(); en.hasMoreElements();) {
                Quadrilatere quad = en.nextElement();
                if (e.getX() > quad.getMinX() + 10 && e.getX() < quad.getMaxX() - 10) {
                    if (e.getY() > quad.getMinY() + 10 && e.getY() < quad.getMaxY() - 10) {
                        if (temp == null) {
                            temp = quad;
                            PointTemporaire = null;
                        } else {
                            if (quad.getZValue() > temp.getZValue()) {
                                temp = quad;
                                PointTemporaire = null;
                            }
                        }
                    }
                }
                if (e.getX() > quad.getA().getX() - 10 && e.getX() < quad.getA().getX() + 10) {
                    if (e.getY() > quad.getA().getY() - 10 && e.getY() < quad.getA().getY() + 10) {
                        temp = quad;
                        PointTemporaire = quad.getA();
                    }
                }
                if (e.getX() > quad.getB().getX() - 10 && e.getX() < quad.getB().getX() + 10) {
                    if (e.getY() > quad.getB().getY() - 10 && e.getY() < quad.getB().getY() + 10) {
                        temp = quad;
                        PointTemporaire = quad.getB();
                    }
                }
                if (e.getX() > quad.getC().getX() - 10 && e.getX() < quad.getC().getX() + 10) {
                    if (e.getY() > quad.getC().getY() - 10 && e.getY() < quad.getC().getY() + 10) {
                        temp = quad;
                        PointTemporaire = quad.getC();
                    }
                }
                if (e.getX() > quad.getD().getX() - 10 && e.getX() < quad.getD().getX() + 10) {
                    if (e.getY() > quad.getD().getY() - 10 && e.getY() < quad.getD().getY() + 10) {
                        temp = quad;
                        PointTemporaire = quad.getD();
                    }
                }
            }
        }
    }

    public void mouseEntered(MouseEvent e) {
        requestFocusInWindow();
    }

    public void mouseExited(MouseEvent e) {
        this.getParent().requestFocusInWindow();
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
        if (temp != null) {
            if(PointTemporaire != null ) {
                PointTemporaire.setX(e.getX());
            PointTemporaire.setY(e.getY());
            this.repaint();
            }
            else {
                temp.deplaceResetA(temp.getA().getX() + (e.getX() - temp.getA().getX()), temp.getA().getY() + (e.getY() - temp.getA().getY()));
            
            this.repaint();
            }
        }
    }

    public void mouseDragged(MouseEvent e) {
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (this.Parent.getControleur().getCollection() != null) {
            for (Enumeration<Quadrilatere> e = this.Parent.getControleur().getCollection().elements(); e.hasMoreElements();) {
                Quadrilatere quad = e.nextElement();
                int[] x = {(int) quad.getB().getX(), (int) quad.getA().getX(), (int) quad.getC().getX(), (int) quad.getD().getX()};
                int[] y = {(int) quad.getB().getY(), (int) quad.getA().getY(), (int) quad.getC().getY(), (int) quad.getD().getY()};
                g.setColor(quad.getCouleur());
                g.fillPolygon(x, y, 4);
            }
        }
        this.Parent.getControleur().action("Canvas", "actualiserListe", null, null);
    }

    public JavaCanvas(IHM parent) {
        super();

        this.Parent = parent;
        this.setBackground(Color.black);
        this.setBorder(BorderFactory.createLineBorder(Color.white));
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        this.addKeyListener(this);
    }
    
    private IHM Parent;
    private Quadrilatere temp;
    private Point PointTemporaire;
}
